package com.one.group.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.one.group.admin.mapper.RepairReportMapper;
import com.one.group.model.dto.RepairReportQueryDto;
import com.one.group.model.dto.UpdateRepairReportDto;
import com.one.group.model.dto.UpdateRepairReportStatusDto;
import com.one.group.model.entity.RepairReport;
import com.one.group.model.vo.RepairReportListVo;
import com.one.group.service.RepairReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author kld
 * @date 2025/9/30 15:28
 * @description TODO
 */
@Service
@Slf4j
public class RepairReportServiceImpl extends ServiceImpl<RepairReportMapper, RepairReport> implements RepairReportService {

    // 获取租户ID的辅助方法
    private Long getCurrentTenantId() {
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id"));
        log.debug("当前租户ID: {}", tenantId);
        return tenantId;
    }

    @Override
    public List<RepairReportListVo> findRepairReportByQueryDto(RepairReportQueryDto repairReportQueryDto) {
        repairReportQueryDto.setTenantId(getCurrentTenantId());
        return this.getMapper().findRepairReportByQueryDto(repairReportQueryDto);
    }

    @Override
    public boolean updateStatus(UpdateRepairReportStatusDto updateRepairReportStatusDto) {
        updateRepairReportStatusDto.setTenantId(getCurrentTenantId());
        // 调用mapper方法获取受影响行数，转换为boolean返回
        boolean affectedRows = this.getMapper().updateRepairReportStatus(updateRepairReportStatusDto);
        return affectedRows;
    }
    
    @Override
    public boolean updateRepairReport(UpdateRepairReportDto updateRepairReportDto) {
        updateRepairReportDto.setTenantId(getCurrentTenantId());
        // 调用mapper方法获取受影响行数，转换为boolean返回
        boolean affectedRows = this.getMapper().updateRepairReport(updateRepairReportDto);
        return affectedRows;
    }
    
    @Override
    public boolean save(RepairReport repairReport) {
        repairReport.setTenantId(getCurrentTenantId());
        return super.save(repairReport);
    }
}
